package com.service.api;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.util.CharsetUtil;

public class HttpResponseHandler extends SimpleChannelUpstreamHandler {
	
	private boolean readingChunks;
	
	public HttpResponseHandler (){
	}
	
	public void messageReceived(ChannelHandlerContext paramChannelHandlerContext,MessageEvent paramMessageEvent) throws Exception {
		Object localObject1;
		Object localObject2;
		if (!(this.readingChunks)) {
			localObject1 = (HttpResponse) paramMessageEvent.getMessage();
			if (!(((HttpResponse) localObject1).getHeaderNames().isEmpty())) {
				localObject2 = ((HttpResponse) localObject1).getHeaderNames().iterator();
			}
			if (((HttpResponse) localObject1).isChunked()) {
				this.readingChunks = true;
			} else {
				localObject2 = ((HttpResponse) localObject1).getContent();
				if (((ChannelBuffer) localObject2).readable()) {
					BaiduApiServer apiServer = CacheManager_app.instance().appMap.get(paramChannelHandlerContext.getChannel().getId());					
					apiServer.callBack(((ChannelBuffer) localObject2).toString(CharsetUtil.UTF_8));
					
				}
			}
		} else {
			localObject1 = (HttpChunk) paramMessageEvent.getMessage();
			if (((HttpChunk) localObject1).isLast()) {
				this.readingChunks = false;
			} else {
				BaiduApiServer apiServer = CacheManager_app.instance().appMap.get(paramChannelHandlerContext.getChannel().getId());
				apiServer.callBack(((HttpChunk) localObject1).getContent().toString(CharsetUtil.UTF_8));
			}
		}
	}
}